package com.czy.project.util;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/********************************************************************
 * @brief
 * @version 0.1
 * @date 2022年09月06日 21:51
 * @author changziyang
 ********************************************************************/
@Data
public class DataResult {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "响应码")
    private Integer code;

    @ApiModelProperty(value = "响应消息")
    private String message;

    @ApiModelProperty(value = "响应数据")
    private Object data;

    //把构造方法私有
    private DataResult() {}

    public static Builder builder() {
        return new Builder();
    }
    public static class Builder{
        private Boolean success;
        private Integer code;
        private String message;
        private Object data;

        public Builder success(Boolean success) {
            this.success = success;
            return this;
        }
        public Builder code(int code) {
            this.code = code;
            return this;
        }
        public Builder message(String message) {
            this.message = message;
            return this;
        }
        public Builder data(Object data) {
            this.data = data;
            return this;
        }

        public DataResult successBuild() {
            DataResult dataResult = new DataResult();
            dataResult.setSuccess(success == null ? true : success);
            dataResult.setCode(code == null ? ResultCode.SUCCESS : code);
            dataResult.setMessage(message == null ? "成功" : message);
            dataResult.setData(data);
            return dataResult;
        }
        public DataResult errorBuild() {
            DataResult dataResult = new DataResult();
            dataResult.setSuccess(success == null ? false : success);
            dataResult.setCode(code == null ? ResultCode.ERROR : code);
            dataResult.setMessage(message == null ? "失败" : message);
            dataResult.setData(data);
            return dataResult;
        }
    }
}
